package com.springboot.controller;

import com.springboot.constant.CommonConstant;
import com.springboot.domain.BtPerson;
import com.springboot.domain.Result;
import com.springboot.dto.TaskAddCaseDTO;
import com.springboot.dto.TaskAddDTO;
import com.springboot.dto.TaskDemandDTO;
import com.springboot.dto.TaskDetailDTO;
import com.springboot.enums.ResultEnum;
import com.springboot.repository.PersonRepository;
import com.springboot.repository.TaskRepository;
import com.springboot.service.TaskService;
import com.springboot.tools.ResultUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Table;
import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping("/bank")
public class TaskController {
    private TaskService taskService;
    private PersonRepository personRepository;
    private TaskRepository taskRepository;

    @Autowired
    public TaskController(TaskService taskService,PersonRepository personRepository,TaskRepository taskRepository) {
        this.taskService = taskService;
        this.personRepository=personRepository;
        this.taskRepository=taskRepository;
    }

    //任务匹配
    @ResponseBody
    @PostMapping(value = "/task/addTask")
    public Result addTask(@Valid @RequestBody TaskAddDTO taskAddDTO, BindingResult bindingResult, HttpSession session){
        if (bindingResult.hasErrors()) {
            List<ObjectError> errorList = bindingResult.getAllErrors();
            for (ObjectError error : errorList) {
                return ResultUtil.error(error.getDefaultMessage());
            }
        }
        String userId = (String) session.getAttribute("user");
        if( userId == null){
            return ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
        if( null == taskAddDTO.getReceiver()){
            taskAddDTO.setReceiver(userId);
        }
        taskService.addTask(taskAddDTO,session);
        return ResultUtil.success("任务匹配成功");
    }

    //任务匹配 个人模糊查询
    @ResponseBody
    @GetMapping(value = "/task/personFuzzyQuery")
    public Result personFuzzyQuery(@RequestParam(value = "name", defaultValue = "",required = false) String name,
                                   @RequestParam(value = "realName", defaultValue = "",required = false) String realName,
                                   @RequestParam(value = "province", defaultValue = "",required = false)  String province,
                                   @RequestParam(value = "profession", defaultValue = "",required = false)  String profession) {
        List<BtPerson> people=personRepository.findByFuzzyQuery(realName,name,province,profession);
        return ResultUtil.success(people);
    }

    //平台task list
    @ResponseBody
    @GetMapping(value = "/task/platForm/taskList")
    public Result taskList(@ApiParam Pageable pageable) {
        Page<TaskDemandDTO>  tasks = taskRepository.findByAll(pageable);
        return ResultUtil.success(tasks);
    }

    //个人/企业 task list
    @ResponseBody
    @GetMapping(value = "/task/person/taskList")
    public Result taskList(@ApiParam Pageable pageable, HttpSession session) {
        String receiver = (String) session.getAttribute("user");
//        String receiver ="1";
        Page<TaskDemandDTO> tasks = taskRepository.findByReceiver(receiver,pageable);
        return ResultUtil.success(tasks);
    }

    @ResponseBody
    @GetMapping(value = "/task/detail/{id}")
    public Result taskDetail(@PathVariable("id") String id, HttpSession session){
        String userId = (String) session.getAttribute("user");
        if( userId == null){
            return ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
       TaskDetailDTO taskDetailDTO = taskService.findTask(id,userId);
        return ResultUtil.success(taskDetailDTO);
    }

    @ResponseBody
    @PostMapping(value = "/task/assure/{id}")
    public Result assureTask(@PathVariable("id") String id, HttpSession session){
        String userId = (String) session.getAttribute("user");
        if( userId == null){
            return ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
       String tip = taskService.assureTask(id, userId);

        if(tip != null){
            return ResultUtil.error(ResultEnum.CONFIRM_FAILED);
        }else {
            return ResultUtil.success("确认成功");
        }

    }

    @ResponseBody
    @PostMapping(value = "/task/reject/{id}")
    @Transactional
    public Result rejectTask(@PathVariable("id") String id, HttpSession session){
        String userId = (String) session.getAttribute("user");
        if( userId == null){
            return ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
        taskRepository.updateStatus(CommonConstant.CLOSE,id);
       return ResultUtil.success("已拒绝");

    }
    //提交方案
    @ResponseBody
    @PostMapping(value = "/task/addCase")
    public Result addCase(@Valid @RequestBody TaskAddCaseDTO taskAddCaseDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<ObjectError> errorList = bindingResult.getAllErrors();
            for (ObjectError error : errorList) {
                return ResultUtil.error(error.getDefaultMessage());
            }
        }
        taskService.taskAddCase(taskAddCaseDTO);
        return ResultUtil.success("提交方案成功");
    }

    //发布者工单未读总数
    @ResponseBody
    @GetMapping(value = "/task/announcerUnReadTotal")
    public Result announcerUnReadTotal(HttpSession session){
        String userId = (String) session.getAttribute("user");
        if( userId == null){
            return ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
       Integer unRead=taskRepository.findUnReadByAnnouncer(userId);
        return ResultUtil.success(unRead);
    }

    //接收者工单未读总数
    @ResponseBody
    @GetMapping(value = "/task/receiverUnReadTotal")
    public Result receiverUnReadTotal(HttpSession session){
        String userId = (String) session.getAttribute("user");
        if( userId == null){
            return ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
        Integer unRead=taskRepository.findUnReadByReceiver(userId);
        return ResultUtil.success(unRead);
    }
}
